package wechat.rcv


import edu.fudan.mylang.pf.IObjectFactory
import org.eclipse.xtend.lib.Property
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.transaction.annotation.Transactional
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import wechat.rcv.util.Log4jService
import wechat.rcv.util.TimeFormatter

@Controller
@Transactional
@RequestMapping("/meetingdetail")
class MeetingDetailController {
	@Autowired
	private IObjectFactory entities;
	
	@Autowired
	private Log4jService log4jService
	
	@RequestMapping(method = RequestMethod::GET)
	def String process(
		@RequestParam(value = "notificationid") Long notificationid, Model model
	) {
		
		log4jService.info(typeof(MeetingDetailController),'notificationid = '+ notificationid)
		var page = 'meeting'
		try{
//			val notification = assertNotNull("invalid notification id",entities.get(typeof(MessageNotification),notificationid))
//			assertNotNull('no message in notification', notification.message)
//			setModelAttribute(model, "notificationid", notificationid.toString);
//			val parkmeeting = (notification.message.subject as ParkMeetingMessage)
//			setModelAttribute(model, "meetingtitle", parkmeeting.title);
//			setModelAttribute(model, "meetingbody", parkmeeting.comment);
//			setModelAttribute(model, "meetingtime", TimeFormatter::timeFormat(parkmeeting.date));
//			setModelAttribute(model, "meetinglocatoin", parkmeeting.location);
//			if(notification.confirmed) setModelAttribute(model, "notificationstatus", "yes");
		}
		catch(WeiXinException re){
			setModelAttribute(model, "errormsg", re.warning)
			log4jService.info(typeof(MeetingDetailController),'Error : '+ re.warning)
			page = 'error'
		}
		return page;
	}
	
	def setModelAttribute(Model model,  String key, String value){
		if ((value!=null)&&(!value.equals("")))
			model.addAttribute(key, value);
	}
	def  <T> T assertNotNull(String msg, T t){
		if(t==null) throw new WeiXinException(msg)
		return t;
	}
}
@Controller
@Transactional
@RequestMapping("/documentdetail")
class DocumentDetailController {
	@Autowired
	private IObjectFactory entities;
	
	@Autowired
	private Log4jService log4jService
	
	@RequestMapping(method = RequestMethod::GET)
	def String process(
		@RequestParam(value = "notificationid") Long notificationid, Model model
	) {
		
		log4jService.info(typeof(DocumentDetailController),'notificationid = '+ notificationid)
		var page = 'document'
		try{
//			val notification = assertNotNull("invalid notification id",entities.get(typeof(MessageNotification),notificationid))
//			assertNotNull('no message in notification', notification.message)
//			setModelAttribute(model, "notificationid", notificationid.toString);
//			val parkdocument = (notification.message.subject as ParkDocumentMessage)
////			setModelAttribute(model, "documenttitle", parkdocument.title);
//			setModelAttribute(model, "documentbody", parkdocument.title + ', '+ parkdocument.comment);
//			if(notification.confirmed) setModelAttribute(model, "notificationstatus", "yes");
		}
		catch(WeiXinException re){
			setModelAttribute(model, "errormsg", re.warning)
			log4jService.info(typeof(DocumentDetailController),'Error : '+ re.warning)
			page = 'error'
		}
		return page;
	}
	
	def setModelAttribute(Model model,  String key, String value){
		if ((value!=null)&&(!value.equals("")))
			model.addAttribute(key, value);
	}
	def  <T> T assertNotNull(String msg, T t){
		if(t==null) throw new WeiXinException(msg)
		return t;
	}
}
@Controller
@Transactional
@RequestMapping("/notificationdetail")
class NotificationDetailController {
	@Autowired
	private IObjectFactory entities;
	
	@Autowired
	private Log4jService log4jService
	
	@RequestMapping(method = RequestMethod::GET)
	def String process(
		@RequestParam(value = "notificationid") Long notificationid, Model model
	) {
		log4jService.info(typeof(DocumentDetailController),'notificationid = '+ notificationid)
		var page = 'notification'
		try{
//			val notification = assertNotNull("invalid notification id",entities.get(typeof(Notification),notificationid))
//			if(	notification instanceof TaskNotification){
//				setModelAttribute(model, "notificationid", notificationid.toString);
////				setModelAttribute(model, "notificationtitle", (notification as TaskNotification).title);
//				setModelAttribute(model, "notificationbody",(notification as TaskNotification).title + '-'+( notification as TaskNotification).body);
//			}else if (notification instanceof MessageNotification)
//			{
//				assertNotNull('no message in notification', (notification as MessageNotification).message)
//				setModelAttribute(model, "notificationid", notificationid.toString);
//				val subject = (notification as MessageNotification).message.subject
////				setModelAttribute(model, "notificationtitle", subject.title);
//				setModelAttribute(model, "notificationbody", subject.title +'-'+subject.comment);
//			}
//			
//			if(notification.confirmed) setModelAttribute(model, "notificationstatus", "yes");
		}
		catch(WeiXinException re){
			setModelAttribute(model, "errormsg", re.warning)
			log4jService.info(typeof(DocumentDetailController),'Error : '+ re.warning)
			page = 'error'
		}
		return page;
	}
	
	def setModelAttribute(Model model,  String key, String value){
		if ((value!=null)&&(!value.equals("")))
			model.addAttribute(key, value);
	}
	def  <T> T assertNotNull(String msg, T t){
		if(t==null) throw new WeiXinException(msg)
		return t;
	}
}

class WeiXinException extends RuntimeException{
	@Property String warning
	new(String msg ){
		warning = msg
	}
}

@Controller
@RequestMapping("/example")
class TaskHistoryController {
	@RequestMapping(method = RequestMethod::GET)
	def String process() {
		return 'example';
	}
}
